package cn.hchaojie.es;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import cn.hchaojie.elasticsearch.dao.ProductDao;
import cn.hchaojie.elasticsearch.domain.Product;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:context.xml")
public class DemoEs2 {
	@Autowired
	ProductDao dao;
	
	
	@Autowired
	ElasticsearchTemplate template;
	
	@Test
	public void testIndexOps() {
		template.createIndex(Product.class);
	}
	
	@Test
	public void testDeleteIndex() {
		template.deleteIndex(Product.class);
	}
	
	// 创建映射
	// elasticsearch里面，直接插入数据的时候，会自动创建索引结构（创建所有的域）
	// 也可以先创建索引结构，再插入数据
	@Test
	public void testMapping() {
		template.putMapping(Product.class);
	}
	
	// 基于关键字的精确匹配
	@Test
	public void testFindByTermQuery() {
		// QueryBuilder qb = QueryBuilders.termQuery("name", "三星笔记本");
		QueryBuilder qb = QueryBuilders.termQuery("name", "三星");
		
		System.out.println(qb);

		Iterable<Product> list = dao.search(qb);

		for (Product p : list) {
			System.out.println(p);
		}
	}
	
	// 基于关键字的精确匹配
	@Test
	public void testFindByTermQuery2() {
		// QueryBuilder qb = QueryBuilders.termQuery("name", "三星笔记本");
		QueryBuilder qb = QueryBuilders.termQuery("category", "笔记本电脑");
		
		System.out.println(qb);

		Iterable<Product> list = dao.search(qb);

		for (Product p : list) {
			System.out.println(p);
		}
	}
	
	// 基于关键字的模糊匹配
	@Test
	public void testFindByWildQuery() {
		QueryBuilder qb = QueryBuilders.wildcardQuery("name", "*电脑");
		
		System.out.println(qb);

		Iterable<Product> list = dao.search(qb);

		for (Product p : list) {
			System.out.println(p);
		}
	}
	
	// 基于搜索条件分词的搜索，支持luecene的搜索语法
	// +name:笔记本 -category:笔记本
	@Test
	public void testFindByQueryString() {
		// 能搜索含有”笔记本电脑“或者”20“这两个关键字其中一个的所有记录
		// QueryBuilder qb = QueryBuilders.queryStringQuery("笔记本电脑20");
		
		QueryBuilder qb = QueryBuilders.queryStringQuery("笔记本电脑20")
				.defaultOperator(Operator.AND);
		
		System.out.println(qb);

		Iterable<Product> list = dao.search(qb);

		for (Product p : list) {
			System.out.println(p);
		}
	}
	
	// 多条件组合查询
	@Test
	public void testBooleanQuery1() {
		// 根据商品名称、分类、还有描述搜索
		QueryBuilder q1 = QueryBuilders.queryStringQuery("笔记本电脑20")
				.analyzer("ik_max_word")
				.field("name")
				.defaultOperator(Operator.OR);
		
		System.out.println(q1);

		Iterable<Product> list = dao.search(q1);

		for (Product p : list) {
			System.out.println(p);
		}
	}
	
	@Test
	public void testBooleanQuery2() {
		// QueryBuilder qb = QueryBuilders.boolQuery();
		BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
		
		// 笔记本电脑  	- 四星笔记本
		// 笔记本电脑20 
		// 。。。
		// 笔记本电脑40
		
		TermQueryBuilder q1 = QueryBuilders.termQuery("category", "笔记本电脑");
		TermQueryBuilder q2 = QueryBuilders.termQuery("category", "20");
		
		boolQuery.must(q1).must(q2);

		Iterable<Product> list = dao.search(boolQuery);

		for (Product p : list) {
			System.out.println(p);
		}
	}
}
